package cn.tedu._02notice.controller;

import cn.tedu._02notice.mapper.NoticeMapper;
import cn.tedu._02notice.pojo.entity.Notice;
import cn.tedu._02notice.pojo.dto.NoticeAddParam;
import cn.tedu._02notice.pojo.dto.NoticeUpdateParam;
import cn.tedu._02notice.pojo.vo.NoticeDetailInfoVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * RestController注解：
 * 1.组合注解，相当于@Controller+@ResponseBody的组合；
 * 2.
 */

@RestController
@RequestMapping("/v1/notice/")
public class NoticeController {
    @Autowired
    private NoticeMapper noticeMapper;

    @PostMapping("add")
    public String add(NoticeAddParam noticeAddparam) {
        System.out.println("noticeAddParam=" + noticeAddparam);
        //将资讯信息插入到数据表中【notice】
        Notice notice = new Notice();
        //复制属性
        BeanUtils.copyProperties(noticeAddparam, notice);
        //处理其他属性
        notice.setUserId(100L);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());

        noticeMapper.insertNotice(notice);
        return "添加成功";
    }


    @PostMapping("detail")
    public NoticeDetailInfoVO detail(long id) {
        System.out.println("id=" + id);
        NoticeDetailInfoVO detailInfo = noticeMapper.DetailInfo(id);

        return detailInfo;
    }

    @DeleteMapping("del")
    public String del(long id) {
        System.out.println("id=" + id);
        int i = noticeMapper.deleteById(id);
        if (i > 0) {
            return "删除成功";
        }
        return "删除失败";
    }

    @PostMapping("update")
    public String update(NoticeUpdateParam noticeUpdateParam) {
        System.out.println("noticeUpdateParam=" + noticeUpdateParam);
        //更新资讯使用实体类
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeUpdateParam, notice);
        //设置更新时间
        notice.setUpdateTime(new Date());
        //调用接口
        int i = noticeMapper.updateNotice(notice);
        return (i > 0 ? "更新成功" : "该资讯不存在");
    }
}

